// ==UserScript==
// @name         send_msg
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  差评单告警消息推送
// @author       You
// @match        andon.cloud.tencent.com/workbench/director/*
// @match        andon.oa.com/workbench/director/*
// @match        andon.woa.com/workbench/director/*
// @grant        none
// @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// @require      https://cdn.jsdelivr.net/npm/@cloudbase/js-sdk@1.7.1/miniprogram_dist/index.js

// ==/UserScript==

(function() {
	Date.prototype.format = function(fmt) {
		var o = {
			"M+" : this.getMonth()+1,	//月份
			"D+" : this.getDate(),	//日
			"h+" : this.getHours(),	//小时
			"m+" : this.getMinutes(),	//分
			"s+" : this.getSeconds()	//秒
		};
		if (/(Y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (let k in o) {
			if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
		return fmt;
	}
	$('body').append(`
	<div id="app-msg">
		<el-row style="position: fixed; z-index: 10;max-width: 150px; top:0; left:0; padding-right: 10px; margin-top: 50px; text-align: right;overflow: auto;max-height: 100vh;" >
			<el-col :span="24" style="margin-bottom: 10px;">
				<el-tooltip class="item" effect="dark" content="点击开始执行" placement="left-start">
					<el-button type="primary" size="mini" @click="AutoTime=1" plain round>{{tips}}</el-button>
				</el-tooltip>
			</el-col>
		</el-row>
	</div>
	`)
	new Vue({
		el:'#app-msg',
		data(){
			return {
				lastDateTime:new Date().format('YYYY-MM-DD 00:00:00'),//上次更新时间
				AutoTime:5*60,//设定定时器任务为5分钟
				doing:false,//是否执行中
				reasonDict:{0: "未 选 择",1: "服务态度",2: "售后技能",3: "解决时效",4: "产品规则",5: "产品能力",6: "产品性能",8: "迁云方案",9: "售前培训",10: "交流拜访"},
				channelDict:{3: "M C",27: "K A",2: "CallCenter",30: "自研",29: "私有化售中/POC测试",28: "私有化售后",4: "助手",5: "邮件",1: "IMCC",10: "回访",12: "投诉/建议",31: "云架平",33: "内部建单(TKE容器/备案等)",32: "安全内部建单",11: "舆情",34: "公有云售中",35: "CSIG知识平台",36: "AMP监控",37: "SaaS产品",38: "举报平台",40: "私有化售后(供应商)",41: "私有化售中/POC测试(供应商)",42: "私有云ASP",43: "政务云",39: "I M",44: "企点BOSS",45: "经营平台",48: "在线售前",46: "微信支付中长尾商户",47: "微信支付KA商户",49: "腾讯会议(售后)",50: "腾讯会议(售前)",52: "微信支付刷脸KA商户群",51: "合作伙伴支持",60: "服务请求"},//
				idsObj:{
					IMids:[27208,27209,27210,27211,27212,27215,27217,27218,27219,27220,27221,27224,27226,27227,27228,27229,27230,27233,27235,27236,27237,27238,27239,27242,27244,27245,27246,27247,27248,27251,27253,27254,27255,27256,27257,27260,27271,27272,27273,27274,27275,27278,27280,27281,27282,27283,27284,27287,27288,27289,27290,27291,27292,27298,27299,27300,27301,27302,27305,27306,27307,27308,27309,27310,27313,27314,27315,27316,27317,27318,27320,27297,27325,27326,27327,27328,27329,27330,27331,27324,27332,27333,27334,27335,27336,27337,35545,35546,35547,35548,35549,35552],
					HDZBids:[11168,11362,11363,11364,11477,13306,11853,11855,11857,13299,11859,13300,11861,13301,11863,11865,13303,14626,11867,13310,13699,13700,13935,12057,13298,14206,12058,13302,14364,14365,14366,14367,14368,12059,13304,12060,13305,12348,13307,14627,14996,15208,12352,13308,12546,13309,13931,13932,13933,15209,19572,19573,14369,14370,14371,14372,14628,14629,14630,14631,15204,13934,15205,15206,15207,19528,19529,19530,19531,19532,19533,19534,19535,19536,19537,19538,19539,19540,19541,19542,19543,19544,19545,19546,19547,19548,19549,19550,19551,19552,19553,19554,19555,19556,19557,19558,19559,19560,19561,19562,19563,19564,19565,19566,19567,19568,19569,19570,19571],
					YDZBids:[12255,12256,13315,12257,13319,12258,13317,12259,13318,14381,14382,14383,14384,19490,19491,19492,19493,19494,19495,19496,19497,19498,19499,19500,19501,19502,19503,19504,12260,13316,12261,13321,12262,13314,15221,12263,13320,12264,13329,13701,14209,14210,12438,13322,12439,13323,12440,13324,12441,13325,12442,13326,12443,13327,12444,13328,14376,14377,14378,14379,14380,14385,14386,14387,14635,14636,14864,14865,14866,14997,15222,15223,15224,15225,15226,19481,19482,19483,19484,19485,19486,19487,19488,19489,19505,19506,19507,19508,19509,19510,19511,19512,19513,19514,19515,19516,19517,19518,19519,19520,19521,19522,19523,19524,19525,19526,19527],
					DSPids:[12552,12553,13330,12554,13331,15227,12555,13332,13705,13706,13707,13708,13923,14211,14212,14388,14389,14390,14391,14392,14393,19629,19630,19631,19632,19633,19634,19635,19636,14394,14395,14396,14397,14398,14399,14632,14633,14634,15228,15229,15230,15231,15232,19616,19617,19618,19619,19620,19621,19622,19623,19624,19625,19626,19627,19628,19637,19638,19639,19640,19641,19642,19643,19644,19645,19646,19647,19648,19649,32434,32436,32438,32440,32442,32444,32446],
					BFQids:[18637,18638,18639,18640,18641,18642,18643,18644,18645,19712,19713,19714,19715,19716,19717,19718,19719,19720,19721,19722,19723,19724,19725,19726,19727,19728,19729,19730,19731,19732,19733,19734,23084,19735,19736,19737,19738,19739,19740,19741,19742,19743,19744,19745,19746,19747,19748,19749,19750,19751,19752,19753,19754],
					TRTCids:[13884,13885,13886,15277,15278,15279,15280,15281,15282,15283,15284,15285,15286,15287,18674,18675,18676,18677,18678,18679,18680,18683,18684,18685,18686,18687,18688,18689,18690,18691,18692,18693,18694,18695,19473,19474,19475,19476,18696,18697,19477,19478,19479,19480,18698,18699,18700,18701,19430,19431,19432,19433,19434,19435,19436,19437,19438,19439,19440,19441,19442,19443,19444,19445,19446,19447,19448,19449,19450,19451,19452,19453,19454,19455,19456,19457,19458,19459,19460,19461,19462,19463,19464,19465,19466,19467,19468,19469,19470,19471,19472,20787,20788,22471,22472,22473,22474,33126,33127,33128,33129,33130,33131,33132,33133,33134,33135,33136,33137,33138,33139,33140,33141,33142,33143,33144,33145,33146,33147,33148,33149,33150,33151,33152,33153,33154,33156,33157,33158,33159,33160,33161,22475,22476,22477,22478,22479,22480,22481,22482,22483,22484,22485,22486,22487,22488,22489,22490,22491,22492,22493,22494,22495,22496,22497,22498,22499,22500,33162,33163,33164,33165,33166,33167,33168,33169,33170,33171,33172,33173,33174,33175,33176,33177,33178,33180,33181,33182,33183,31469,31470,31471,31472,31473,31474,31475,31476,32110,32111,32112,32113,32114,32115,32116,32117,32695,32696,46875,46881,46882,46883,46884,46885,46886,46887,46888,46889,46876,46890,46891,46892,46893,46894,46895,46896,61675,46877,46897,46898,46899,46900,46901,46902,46903,46904,46905,46906,46907,46908,46909,46910,46911,46912,46878,46913,46914,46915,46916,46917,46918,46919,46879,46920,46921,46922,46923,46924,46925,46926,46927,46928,46880,46929,46930,46931,46932,46933,46934,46935,46936,46937,46938,46939,46940,46941,46942,61013,61014,61015,61016,61017,61018,61019,61020,61021,61022,61023,61024,61025,61026,61027,61028,61029,61030,61031,61032,61033,61034,61035,61036,61037,61038,61039,61040,61041,61042,61043,61044,61045,61046,61047,61048,61049,61050,61051,61052,61053,61054,61055,61056,61058,61059,61060,61061,61062,61063,61064,61065,61066,61067,61068,61069,61070,61071,61072,61073,61074,61075,61076,61077,61078,61079,61080,61081,61082,61083,61084,61085,61086,61087,61088,61089,61090,61091,61092,61093,61094,61095,61096,61097,61098,62406,62407],
					TCBids:[17481,17482,17483,17922,17925,17926,17927,17928,17923,17924,36716,36717,36718,36719,36720,36721,36722,41079,42456,20778,20779,20780,20781,36700,36701,36702,36703,36704,36705,36706,36707,36708,36709,36710,41070,41071,41072,41073,41074,41075,41076,51983,51984,20782,20783,22856,22857,33481,33482,33483,33484,33485,33993,33994,33995,33996,33997,33998,33999,34000,34366,34367,34368,34369,34370,34371,34372,35790,35800,35801,35802,35803,35804,35805,35806,35807,35887,35888,35889,35890,35891,35892,36592,36593,36594,36595,36596,36597,36598,36711,36712,36713,36714,36715,41077,41078,47384,36723,36724,36725,36726,36727,36728,36729,36730,36731,36732,36733,36734,36735,36736,36737,36738,36739,36740,41080,41081,41082,41083,41084,41085,41086,41087,41088,41089,42457,42458,42459,52422,52423,52424,52425,52656,52657,52658,52659,52660,52661,37168,37169,37170,37171,37172,37173,37174,37175,37176,37304,37305,37306,37312,37975,37976,37977,37978,38009,38010,38011,38012,38013,38026,38027,38028,38029,38030,38357,38359,38360,38361,38362,38363,41090,41091,41092,41093,41094,41095,41096,41097,41098,41099,41100,41101,41102,41103,41104,41105,41106,52662,52663,52664,52665,41107,41108,41109,47512,48029,48030,48031,52666,52667,52668,52669,52670,52671,52672,52673,52674,52675,52676,52677,52678,52679,52680,52681,52682,52683,52684,52685,52686,52687,52688,52689,52690,52691,52692,52693,52694,52695,52696,52697,52698,52699,52700,52701,52702,52703,52704,52705,52706,52707,52708,52709,52710,52711,52712,52713,52714,52715,52716,52717],
				}
			};
		},
		computed:{
			tips(){
				return this.doing?`执行中(${-this.AutoTime}秒)...`:`剩${Math.floor(this.AutoTime/60)}分${this.AutoTime%60}秒后执行`;
			},
			cpList(){
				let arr = [];
				for (let key in this.idsObj) {
					arr = [...arr,...this.idsObj[key]];
				}
				return arr;
			}
		},
		async created(){
			this.$tcb = cloudbase.init({env:'andon-5gwcr58lab678cdf'});
			this.$auth = this.$tcb.auth();
			await this.$auth.anonymousAuthProvider().signIn().then(() => {console.log('%c匿名登录成功','color:#4CAF50;')});
			this.$db = this.$tcb.database();
			this.lastDateTime = await this.$db.collection("config").where({'code':'lastDateTime'}).get().then(res=>res.data[0].value);
			setInterval(async()=>{
				this.AutoTime --;
				if(this.AutoTime == 0 && !this.doing) {
					this.doing = true;
					await this.start();
					this.AutoTime = 5*60;
					this.doing = false;
				}
			},1000);//一秒一次
		},
		methods:{
			async ajax(d) { //网络请求
				let cg = {headers: {'Content-Type': d['Content-Type']||'application/json'},method: d.type || 'GET'};
				if (d.cookie) cg.credentials = "include";
				if (d.type == 'POST') cg.body = d.body||JSON.stringify(d.data || {});
				return (await fetch(d.url, cg)).json();
			},
			async start() { //开始函数
				let badDatas = [];
				try{
					let nowDate = new Date().format('YYYY-MM-DD hh:mm:ss');
					let mcDatas = await this.ajax({url:'/ticket/api/tickets',cookie:true,type:'POST',data:{
						status: [3],//已结单
						service_scene:this.cpList,//终端五组产品
						close_time_start:this.lastDateTime,//结单开始时间
						close_time_end:nowDate,//结单结束时间
						page: 1,limit: 1000,
					}}).then(res=>res.data.data);
					for(let obj of mcDatas){
						let opArr = obj.operators.split(',');
						let operator = obj.service_channel == 27 ? opArr[opArr.length-1]:opArr[0];//负责人取值，大客户单独规则
						let defult = {id:obj.ticket_id,closeTime:obj.close_time,appraise:obj.appraise,operator,isTCB:obj.service_scene[1].id==17481,scene:obj.service_scene.map((v,i)=>{return i==0?'':v.name}).join('>').substr(1)};
						if(obj.service_channel == 39){//会话单
							let userid = localStorage.userid || await this.ajax({url:'/user?scope=ONLINE'}).then(res=>{return localStorage.userid = res.data.id;});
							let d = await this.ajax({url:'/trpc.andon.csc.CSC/GetOverConversations',cookie:true,type:'POST',data:{
								"Page": 1,"Limit": 10,
								"TicketId": obj.ticket_id+"",
								"ReqComm": {"LoginUid": userid},
							}}).then(res=>res.Conversations[0])||{};
							if(d.ServiceRate>0 && d.ServiceRate < 9){//小于9是差评
								let url = await this.getUrl(obj.ticket_id,obj.ticket_sign);
								badDatas.push({...defult,rete:d.ServiceRate,type:'会话',reason:this.reasonDict[d.ConversationUnsatisfyReason||(d.ProductUnsatisfyReason-0+3)],url});
							}
						}else{//其它类型
							if(obj.service_rate>=1 && obj.service_rate<=4){//差评单
								let url = await this.getUrl(obj.ticket_id,obj.ticket_sign);
								badDatas.push({...defult,rete:obj.service_rate,type:this.channelDict[obj.service_channel],reason:this.reasonDict[obj.unsatisfy_reason],url});
							}
						}
					}
					await this.$tcb.callFunction({name:'sendMsg',data:{nowDate,badDatas}}).then(res=>console.log(res));
					if(badDatas.length>0) this.$message({message: `已成功推送${badDatas.length}条差评`,type: 'success',duration:0,showClose:true});
					this.lastDateTime = nowDate;//更新时间
				}catch(e){
					console.error(e)
					this.$message({message: '处理异常，请及时处理。',type: 'error',duration:0,showClose:true});
				}
			},
			async getUrl(id,sign){
				return await this.ajax({url:`/ticket/api/redirect/genRedirectUrl?id=${id}&sign=${sign}`}).then(res=>res.data.url);
			}
		}
	});
})();